package com.travelsmart.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.travelsmart.client.apresentacao.image.ImagesResource;
import com.travelsmart.client.apresentacao.mvc.MVCHotel;
import com.travelsmart.client.apresentacao.screen.ConsultarReservasHotelScreen;
import com.travelsmart.client.apresentacao.screen.RealizarReservaHotelScreen;
import com.travelsmart.client.apresentacao.screen.ScreenHome;
import com.travelsmart.client.apresentacao.widget.FormMenuBar;
import com.travelsmart.client.control.AdminService;
import com.travelsmart.client.control.AdminServiceAsync;
import com.travelsmart.client.control.HotelSmartService;
import com.travelsmart.client.control.HotelSmartServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TravelSmart implements EntryPoint {

	//
	public void onModuleLoad() {
		((ServiceDefTarget) HotelSmartServiceAsync.service)
				.setServiceEntryPoint(GWT.getModuleBaseURL()
						+ HotelSmartService._RELATIVE_PATH);
		((ServiceDefTarget) AdminServiceAsync.service)
		.setServiceEntryPoint(GWT.getModuleBaseURL()
				+ AdminService._RELATIVE_PATH);
		RootPanel.get().setWidth("100%");
		RootPanel.get().add(createMainPanel());
		MVCHotel.criarAdminViews();
	}

	public DockPanel createMainPanel() {
		final DockPanel mainPanel = new DockPanel();
		final DeckPanel centerPanel = new DeckPanel();
		mainPanel.setWidth("100%");
		mainPanel.setHeight("100%");
		mainPanel.setSpacing(4);
		criarMargemEsquerda(mainPanel);
		criarMargemDireita(mainPanel);
		criarCabecalho(mainPanel);
		criarMenu(mainPanel, centerPanel);
		criarMargemEsquerda(mainPanel);
		criarMargemDireita(mainPanel);
		montarCentro(mainPanel, centerPanel);
		return mainPanel;
	}

	private void criarMenu(final DockPanel mainPanel,
			final DeckPanel centerPanel) {
		mainPanel.add(new FormMenuBar(centerPanel), DockPanel.NORTH);
	}

	private void montarCentro(final DockPanel mainPanel, final DeckPanel centerPanel) {
		addScreen(centerPanel);
		centerPanel.showWidget(0);
		centerPanel.setWidth("100%");
		centerPanel.setHeight("1px");
		mainPanel.add(centerPanel, DockPanel.CENTER);
	}

	private void criarMargemDireita(DockPanel mainPanel) {
		final HorizontalPanel eastPanel = new HorizontalPanel();
		eastPanel.setWidth("100px");
		eastPanel.add(new HTML("<!-- east -->"));
		mainPanel.add(eastPanel, DockPanel.EAST);
		configWindow(eastPanel);
	}

	private void criarMargemEsquerda(DockPanel mainPanel) {
		final HorizontalPanel west = new HorizontalPanel();
		west.setWidth("100px");
		west.add(new HTML("<!-- west -->"));
		mainPanel.add(west, DockPanel.WEST);
		configWindow(west);
	}

	private void configWindow(final HorizontalPanel west) {
		Window.addResizeHandler(new ResizeHandler() {
			@Override
			public void onResize(ResizeEvent event) {
				onWindowResized(west, event.getWidth(), event.getHeight());
			}
		});
		Window.enableScrolling(true);
		Window.setMargin("0px");
		onWindowResized(west, Window.getClientWidth(), Window.getClientHeight());
	}

	private void criarRodape(DockPanel mainPanel) {
		VerticalPanel southPanel = new VerticalPanel();
		southPanel.setStyleName("travelsmart-footer");
		southPanel.setSize("100%", "20px");
		mainPanel.add(southPanel, DockPanel.SOUTH);
	}
	private void criarCabecalho(DockPanel mainPanel) {
		HorizontalPanel northPanel = new HorizontalPanel();
		northPanel.setWidth("100%");
		northPanel.setStyleName("travelsmart-header");
		northPanel.setSize("100%", "67px");
		northPanel.setSpacing(1);
		Image logo = new Image(ImagesResource.INSTANCE.logo2());
		northPanel.add(logo);
		mainPanel.add(northPanel, DockPanel.NORTH);
	}

	/**
	 * adiciona os Screen no DeckPanel
	 */
	private void addScreen(DeckPanel centerPanel) {
		ScreenHome home = new ScreenHome();
		centerPanel.add(home);
		
		
		/*/# INICIO - EXERC. 1*/
		ConsultarReservasHotelScreen consultarReservasHotelScreen = MVCHotel.criarConsultarReservasHotelScreen();		
		centerPanel.add(consultarReservasHotelScreen);
		//FIM - EXERC. 1 #*/
		

		// INICIO - EXERC. 3
		RealizarReservaHotelScreen realizarReservaHotelScreen = MVCHotel.criarRealizarReservaHotelScreen();
		centerPanel.add(realizarReservaHotelScreen);
		//FIM - EXERC. 3	
		
	}

	
	private void onWindowResized(HorizontalPanel panel, int width, int height) {
		int shortcutHeight = height - panel.getAbsoluteTop() - 10;
		if (shortcutHeight < 1) {
			shortcutHeight = 1;
		}
		panel.setHeight(shortcutHeight + "px");
	}
}
